//=============================================================================
// System  : MatchKit
// File    : MatchLocation.cs
// Author  : Fabio Bussu  (matchkit@fabiobussu.it)
// Updated : 04/16/2013
// Note    : Copyright 2013, Fabio Bussu, All rights reserved
//
// This code is published under the Microsoft Public License (Ms-PL). A copy
// of the license should be distributed with the code.  It can also be found
// at the project website: http://matchkit.codeplex.com. This notice, the
// author's name, and all copyright notices must remain intact in all
// applications, documentation, and source files.
//
// Version     Date     Who  Comments
// ============================================================================
// 1.0.0.0  04/16/2013   FB  First draft
//=============================================================================
using System;
using System.Runtime.Serialization;

namespace MatchKit
{
	/// <summary>
	/// Represents a specific location inside a string.
	/// </summary>
	/// <threadsafety static="true" instance="false" />
	/// <serialization binary="true" contract="true" />
	/// <conceptualLink target="0a06b2a9-5f58-409a-ba62-639cfd77bca7" />
	#if DC
	[DataContract]
	#endif
	#if SZ
	[Serializable]
	#endif
	public struct MatchLocation
	{
		/// <summary>
		/// Gets the line number.
		/// </summary>
		/// <serialization member="true" />
		#if DC
		[DataMember]
		#endif
		public readonly int Line;

		/// <summary>
		/// Gets the column number.
		/// </summary>
		/// <serialization member="true" />
		#if DC
		[DataMember]
		#endif
		public readonly int Column;

		/// <summary>
		/// Gets the absolute index position.
		/// </summary>
		/// <serialization member="true" />
		#if DC
		[DataMember]
		#endif
		public readonly int Index;

		/// <summary>
		/// Initializes a new instance of the <see cref="T:MatchKit.MatchLocation"/> struct. 
		/// </summary>
		/// <param name="line">The line number. This value cannot be negative.</param>
		/// <param name="column">The column number. This value cannot be negative.</param>
		/// <param name="index">The absolute index. This value cannot be negative.</param>
		public MatchLocation(int line, int column, int index)
		{
			if (line < 0)
				throw new ArgumentException("line cannot be less than 0", "line");
			if (column < 0)
				throw new ArgumentException("column cannot be less than 0", "column");
			if (index < 0)
				throw new ArgumentException("index cannot be less than 0", "index");
			Line = line;
			Column = column;
			Index = index;
		}

		/// <inheritdoc />
		public override int GetHashCode()
		{
			var h = Line.GetHashCode();
			h ^= Column.GetHashCode();
			h ^= Index.GetHashCode();
			return h;
		}

		/// <inheritdoc />
		public override bool Equals(object obj)
		{
			if (obj == null)
				return false;
			var loc = (MatchLocation)obj;
			return (loc.Index == Index)
				&& (loc.Column == Column)
				&& (loc.Line == Line);
		}

		/// <summary>
		/// Performs an equal comparison between two locations.
		/// </summary>
		/// <param name="a">First location.</param>
		/// <param name="b">Second location.</param>
		/// <returns>The result of the comparison.</returns>
		public static bool operator ==(MatchLocation a, MatchLocation b)
		{
			if (a == null)
				throw new ArgumentNullException("a", "a cannot be null");
			return a.Equals(b);
		}

		/// <summary>
		/// Performs a not equal comparison between two locations.
		/// </summary>
		/// <param name="a">First location.</param>
		/// <param name="b">Second location.</param>
		/// <returns>The result of the comparison.</returns>
		public static bool operator !=(MatchLocation a, MatchLocation b)
		{
			if (a == null)
				throw new ArgumentNullException("a", "a cannot be null");
			return !a.Equals(b);
		}
	}
}
